package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhang.mapper.IInputCarMapper;
import com.zhang.pojo.InputCarBean;
import com.zhang.pojo.InputInfoBean;
import com.zhang.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class InputCarServiceImpl implements IInputCarService {

    @Autowired
    private IInputCarMapper inputCarMapper;
    @Autowired
    private IInputInfoService inputInfoService;
    @Autowired
    private IProductService productService;
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private IUserService userService;

    @Override
    public List<InputCarBean> findBySupplierAndUser(int supplierId, int userId) {
        QueryWrapper<InputCarBean> wrapper = new QueryWrapper<>();
        wrapper.eq("fk_supplierId",supplierId);
        wrapper.eq("fk_userId",userId);
        return inputCarMapper.selectList(wrapper);
    }

    @Override
    public void add(InputCarBean inputCarBean) {
        inputCarMapper.insert(inputCarBean);
    }

    @Override
    public void del(int id) {
        inputCarMapper.deleteById(id);
    }

    @Override
    public void delAll(int supplierId, int userId) {
        //采购人
        String userName = userService.findById(userId).getReal();
        //供应商名称
        String supplierName = supplierService.findById(supplierId).getName();
        //对应入库购物车集合
        List<InputCarBean> inputCarBeans = findBySupplierAndUser(supplierId, userId);

        inputInfoService.add(new InputInfoBean(supplierName,userName),inputCarBeans);
        productService.add(inputCarBeans);

        UpdateWrapper<InputCarBean> wrapper = new UpdateWrapper<>();
        wrapper.eq("fk_supplierId",supplierId);
        wrapper.eq("fk_userId",userId);
        inputCarMapper.delete(wrapper);
    }

    @Override
    public void update(int inputCarId, int number) {
//        InputCarBean inputCarBean = new InputCarBean();
//        inputCarBean.setCount(number);
//        UpdateWrapper<InputCarBean> wrapper = new UpdateWrapper<>();
//        wrapper.eq("pk_inputCarId",inputCarId);
//        inputCarMapper.update(inputCarBean,wrapper);
        inputCarMapper.update(inputCarId, number);
    }
}
